package com.sam.designpatterns


abstract class Computer {
    var mainBoard: String? = null // 主板
    var display: String? = null // 显示器
    var OS: String? = null // 操作系统，null 时未安装操作系统

    //安装操作系统
    abstract fun installOS()
    override fun toString(): String {
        return "Computer info: (mainBoard=$mainBoard, display=$display, OS=$OS)"
    }
}

class MacBook : Computer() {
    override fun installOS() {
        OS = "macOS Big Sur 11.0.1"
    }
}

abstract class Builder {
    // 安装主板
    abstract fun setupMainBoard(mainBoard: String): Builder
    // 连接显示器
    abstract fun connectDisplay(display: String): Builder
    // 安装操作系统
    abstract fun installOS(): Builder
    // 创建一台装好的电脑
    abstract fun create(): Computer
}

class MacBookBuilder : Builder() {
    val computer = MacBook()

    override fun setupMainBoard(mainBoard: String): Builder {
        computer.mainBoard = mainBoard
        return this
    }

    override fun connectDisplay(display: String): Builder {
        computer.display = display
        return this
    }

    override fun installOS(): Builder {
        computer.installOS()
        return this
    }

    override fun create(): Computer {
        return computer
    }
}

class Director(val builder: Builder) {
    fun constructMackBook(mainBoard: String, display: String) {
        builder.setupMainBoard(mainBoard)
                .connectDisplay(display)
                .installOS()
    }
}

fun main() {
    val builder = MacBookBuilder()
    val director = Director(builder)
    // 来个 M2 + 4K 的 MackBook 一定很爽
    director.constructMackBook("M2", "4K 显示器")
    // 最后打印一下组件好的电脑信息
    print(builder.create())

//    MacBookBuilder().setupMainBoard(mainBoard)
//            .connectDisplay(display)
//            .installOS()
//            .create()
}